package edu.nf.bs.util;
import com.alipay.api.internal.util.StringUtils;
import edu.nf.bs.vo.UserAddressVO;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;


/**
 * 经纬度转换测试
 */
public class BaiDuMapUnit {
    /**
     * 输入地址返回经纬度坐标 key lng(经度),lat(纬度)
     */
    public static UserAddressVO getGeocoderLatitude(String address) {
        BufferedReader in = null;
        UserAddressVO userAddressVO=null;
        try {
            address = URLEncoder.encode(address, "UTF-8");
            URL tirc = new URL("http://api.map.baidu.com/geocoder/v2/?address=" + address + "&output=json&ak="
                    + "wws9Qu73jw4QkOL6osEyIsA9Yob2yYgR"+"&callback=showLocation");
            in = new BufferedReader(new InputStreamReader(tirc.openStream(), "UTF-8"));
            String res;
            StringBuilder sb = new StringBuilder("");
            while ((res = in.readLine()) != null) {
                sb.append(res.trim());
            }
            String str = sb.toString();
            if (!StringUtils.isEmpty(str)) {
                int lngStart = str.indexOf("lng\":");
                int lngEnd = str.indexOf(",\"lat");
                int latEnd = str.indexOf("},\"precise");
                if (lngStart > 0 && lngEnd > 0 && latEnd > 0) {
                    String lng = str.substring(lngStart + 5, lngEnd);
                    String lat = str.substring(lngEnd + 7, latEnd);
                    System.out.println("lng:" + lng + "    lat:" + lat);
                    userAddressVO=new UserAddressVO(lng,lat);

                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return userAddressVO;
    }

    /**
     * 输入经纬度返回地址 key lng(经度),lat(纬度)
     */
    public static void getPosition(String latitude, String longitude) throws MalformedURLException {
        BufferedReader in = null;
        URL tirc = new URL("http://api.map.baidu.com/geocoder/v2/?callback=renderReverse&location=" + latitude + "," + longitude
                + "&output=json&pois=1&ak=" + "wws9Qu73jw4QkOL6osEyIsA9Yob2yYgR");
        try {
            in = new BufferedReader(new InputStreamReader(tirc.openStream(), "UTF-8"));
            String res;
            StringBuilder sb = new StringBuilder("");
            while ((res = in.readLine()) != null) {
                sb.append(res.trim());
            }
            String str = sb.toString();
            System.out.println(str);
            if (!StringUtils.isEmpty(str)) {
                int lngStart = str.indexOf("formatted_address\":\"");
                int lngEnd = str.indexOf("\",\"business");
                if (lngStart > 0 && lngEnd > 0 ) {
                    String ads = str.substring(lngStart + 20, lngEnd);
                    System.out.println("ads:" + ads);
                }
            }

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

//    public static void main(String[] args) throws MalformedURLException {
//        // TODO Auto-generated method stub
//        UserAddressVO vo = BaiDuMapUnit.getGeocoderLatitude("广东省珠海市斗门区广东南方职业培训学院");
//        System.out.println(vo);
//        BaiDuMapUnit.getPosition("22.556585037727996", "114.05420498035405");
//    }
}